<?php
	$pageTitle ="Vacation Packages";
	include("gpagetop.php");
	
	function connect() //basic database connect, revise to suit a universal user once one is chosen -- Fraser
		{
		$dbh = @mysqli_connect("localhost", "user", "password", "travelexperts")
			or die("Server Failed to Connect: " . mysqli_connect_error());
		return $dbh;
		}
	function getPackages() //prints table rows for each package that has started but not ended -- Fraser
		{
		$cxn = connect();
		$day = date('d');
		$month = date('m');
		$year = date('Y');
		$date = $year."-".$month."-".$day;
		$sql = "SELECT * 
				FROM `packages` 
				WHERE `PackageId` IS NOT NULL 
				AND `PkgName` IS NOT NULL 
				AND `PkgStartDate` IS NOT NULL 
				AND `PkgEndDate` >= '$year-$month-$day 00:00:00'
				AND `PkgDesc` IS NOT NULL AND `PkgBasePrice` IS NOT NULL
				AND `PkgAgencyCommission` IS NOT NULL";
		
		$result = mysqli_query ($cxn, $sql);
		$string="";
		$i=0;//incrementer for alternating background color -- Fraser
		while($row = mysqli_fetch_array ($result))//reads query line by line saves in $row array-- Fraser
			{
			//droping time from server date info -- Fraser
			$startString = explode(' ', $row[2], 2); //removes time from date --fraser
			$startDate = explode('-', $startString[0], 3); 
			$startYear = $startDate[0];
			$startMonth = $startDate[1];
			$startDay= $startDate[2];
			$started = $startYear. "-" . $startMonth. "-" .$startDay;
			
			$price = $row[5]+$row[6];//Add base price and commission --Fraser
			$price = explode('.', $price, 2);//remove decimals --fraser
			
			$endString = explode(' ', $row[3], 2); //remove time from end date--fraser
			
			if($started <= $date)//check if package has started
				{
				$style = 'style="color:FireBrick"';//change text to red if started
				}
			else
				{
				$style='';
				}
			if($i%2==0)
				{
				$bkgd= 'style="background-color:SkyBlue;"';
				$i= $i+1;
				}
			else
				{
				$bkgd ='';
				$i= $i+1;
				}
			// all packages will appear on one line in html, time permiting I may research how to fix this for clarity --Fraser
			$string .= "<tr $style $bkgd><form method='post' action='confirmation.php'>
							<td><input type='hidden' name='PkgName' value='$row[1]'>$row[1]</td>
							<td><input type='hidden' name='PkgDesc' value='$row[4]'>$row[4]</td>
							<td><input type='hidden' name='PkgStartDate' value='$startString[0]'>$startString[0]</td>
							<td><input type='hidden' name='PkgEndDate' value='$endString[0]'>$endString[0]</td>
							<td><input type='hidden' name='PkgPrice' value='$price[0]'>$price[0]</td>
							<td>
								<input type='hidden' name='PackageId' value='$row[0]'>
								<input type='submit' value='Order'>
							</td>
						</form></tr>";
			}
			
		return $string;
	}
?>

<!-- where Packages are displayed - Fraser -->
<div class="center">
	<br />
	<br />
	<table>
		<tr>
			<td><h3>Pacakge</h3></td>
			<td> </td>
			<td><h3>Start</h3></td>
			<td><h3>End</h3></td>
			<td><h3>Price</h3></td>
		</tr>
		<?php 
			$html = getPackages(); 
			print("$html");
		?>
	</table>
</div>
	
<?php include("gpagebottom.php")?>